package springJDBCsrc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlQuery;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

/**
 * Created by whyang on 2017/03/27.
 */
/*
public class UserModelMappingSqlQuery extends SqlQuery<UserModel> {
    public UserModelMappingSqlQuery(JdbcTemplate jdbcTemplate){
    }

    @Override
    public RowMapper<UserModel> newRowMapper(Object[] objs, Map map){
        return new UserRowMapper();
    }

}*/

public class UserModelMappingSqlQuery extends MappingSqlQuery<UserModel> {
    public UserModelMappingSqlQuery(JdbcTemplate jdbcTemplate) {
        super.setDataSource(jdbcTemplate.getDataSource());
        super.setSql("select * from test where name=:name");
        super.declareParameter(new SqlParameter("name", Types.VARCHAR));
        compile();
    }

    @Override
    protected UserModel mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserModel model = new UserModel();
        model.setId(rs.getInt("id"));
        model.setMyName(rs.getString("name"));
        return model;
    }
}
